package com.ssmdemo.service.imp.empimpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ssmdemo.dao.UserMapping;
import com.ssmdemo.pojo.BusinessException;
import com.ssmdemo.pojo.User;
import com.ssmdemo.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class UserServiceImp extends ServiceImpl<UserMapping, User> implements UserService {

    public User login(String username,String password) throws BusinessException{

        User user=getLoginName(username);

        if (user==null){
            throw new BusinessException(100,"用户不存在");
        }

        if (!user.getPassword().equals(password)) {
//            System.out.println(new BusinessException(101,"用户不存在或密码错误").getMsg());
            throw new BusinessException(101,"用户不存在或密码错误");
        }

        return user;
    }


    public User getLoginName(String username) {

        EntityWrapper<User> entityWrapper=new EntityWrapper<User>();
        entityWrapper.eq("username",username);
        List<User> list=baseMapper.selectList(entityWrapper);

        if (list.isEmpty()){
            return null;
        }else {
            return list.get(0);
        }
    }

    public Set<String> getPermissionCodeById(Integer id) {

        Set<String> stringSet=baseMapper.getPermissionCodeById(id);

        return stringSet;
    }
}
